<template>
    <div class="add_content">
        <div class="add" @click="showType = !showType">
            <i class="icon-plus"></i>
        </div>
        <div class="add_type" v-if="showType">
            <div class="item" @click="activeFn('txt')">
                <i class="icon-wenzhang1" style="background: #eaf7ff;color: #17bceb"></i>
                <p>文字</p>
            </div>
            <div class="item" @click="activeFn('img')">
                <i class="icon-tianjiatupian" style="background: #f3f3fd;color: #5862d4"></i>
                <p>图片</p>
            </div>
            <div class="item" @click="activeFn('video')">
                <i class="icon-shipinzhibo" style="background: #fce7d9;color: #f1680b"></i>
                <p>视频</p>
            </div>
            <div class="item" @click="activeFn('advert')">
                <i class="icon-advert" style="background: #d9fcf6;color: #01a745"></i>
                <p>广告</p>
            </div>
        </div>
        <div class="enter_word" v-if="showTxtEnter">
            <div class="text">
                <textarea maxlength="-1" placeholder="请输入文字" v-model="txtContent" placeholder-class="placeholder"></textarea>
                <div class="txt_limit"><span class="mar-r10">{{txtContent.length}}</span>/{{txtLimit}}</div>
            </div>
            <div class="save_often mar-b10">
                <label class="flex-v-center" @click="saveOften = !saveOften">
                    <radio style="zoom:0.8" :checked="saveOften" />
                    存为常用
                </label>
            </div>
            <div class="btns flex">
                <div class="btn" @click="cancelEnterTxt">取消</div>
                <div class="btn" @click="confirmEnterTxt">确定</div>
            </div>
            <div class="often_use">
                <div class="title flex">
                    <span> 常用文字 </span>
                    <i @click="deleteOften('all')" class="icon-delete3">清空</i>
                </div>
                <div class="content">
                    <div class="item" v-for="(i, idx) in ofenTxtList" :key="idx">
                        <p @click="txtContent += i" >{{i}}</p>
                        <i @click="deleteOften(idx)" class="icon-close"></i>
                    </div>
                </div>
            </div>
        </div>

        <uni-popup ref="copyVideo" type="bottom">
            <div class="video_box">
                <i class="icon-close" @click="closeVideoPopup"></i>
                <p class="title">添加网络视频</p>
                <textarea maxlength="-1" v-model="videoUrl" placeholder="请在此处粘贴视频链接"></textarea>
                <div class="btns">
                    <div class="btn" @click="confirmVideo">确认添加</div>
                </div>
                <div class="tip">目前支持：腾通讯视频、优酷视频、哔哩哔哩（B站）视频，3个平台的视频</div>
                <div class="how_to_use">
                    <p>复制方法：</p>
                    <p>1. 点击视频页面的“分享”按钮，并找到“复制链接”选项点击复制。</p>
                    <p>2. 讲复制的练级“粘贴”到此处的输入框内，点击“确认添加”即可。</p>
                </div>

            </div>

        </uni-popup>
        <div class="mask" @click="showType = !showType" v-if="showType"></div>
        <adSelecter ref="adSelecter" type="文章"></adSelecter>
    </div>
</template>

<script>
import apiList from '@/request/apiList'
import adSelecter from '@/components/pages/adSelecter'
export default {
    components: {
        adSelecter
    },
    data() {
        return {
            showType: false,

            showTxtEnter: false,
            saveOften: false,
            ofenTxtList: [],
            txtContent: '',
            txtLimit: 8000,

            videoUrl: ''
        }
    },
    props: {
        editText: {
            type: Boolean,
            default: true
        }
    },
    methods: {
        activeFn(type) {
            this.showType = false
            if(type == 'txt') {
                if(this.editText) {
                    this.showTxtEnter = true
                    this.ofenTxtList = uni.getStorageSync('ofenTxtList') || []
                } else {
                    this.$emit('active', {
                        type: 'txt',
                        content: ''
                    })
                }
            }
            if(type == 'img') {
                this.chooseImg()
            }
            if(type == 'video') {
                this.$refs.copyVideo.open()
            }
            if(type == 'advert') {
                this.$refs.adSelecter.open().then(rst => {
                    console.log(rst);
                    this.$emit('active', {
                        type: 'advert',
                        content: rst.id
                    })
                })
            }
        },
        confirmEnterTxt() {
            if(!this.txtContent || !this.txtContent.trim()) return this.$interact.toast('请输入内容')
            if(this.txtContent.length > this.txtLimit) return this.$interact.toast('一段文字请不要超过' + this.txtLimit+ '个字符')
            if(this.saveOften) {
                this.ofenTxtList.push(this.txtContent)
                uni.setStorageSync('ofenTxtList', this.ofenTxtList)
            }
            this.$emit('active', {
                type: 'txt',
                content: this.txtContent
            })
            this.cancelEnterTxt()
        },
        async deleteOften(index) {
            let action = await this.$interact.modal({
                title: '提示',
                content: index == 'all' ? '确定清空？' :'确定删除？',
                showCancel: true,
            })
            if(!action.confirm) return;
            if(index == 'all') {
                this.ofenTxtList = []
                uni.removeStorageSync('ofenTxtList')
            } else {
                this.ofenTxtList.splice(index, 1)
                uni.setStorageSync('ofenTxtList', this.ofenTxtList)
            }
        },
        cancelEnterTxt() {
            this.showTxtEnter = false
            this.txtContent = ''
            this.saveOften = false
        },
        chooseImg() {
            uni.chooseImage({
                success: (chooseImageRes) => {
                    const tempFilePaths = chooseImageRes.tempFilePaths;
                    uni.showLoading({ mask: true })
                    console.log(tempFilePaths[0], 'ddddddd3333');
                    uni.uploadFile({
                        url: apiList.baseUrl + apiList.base.uploadFile, //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            fileType: 'img',
                            fileName: 'file',
                            token: this.$store.state.user.account.token,
                        },
                        success: res => {
                            res = JSON.parse(res.data)
                            if(res.code == 1) {
                                this.$emit('active', {
                                    type: 'img',
                                    content: res.data.url
                                })
                            } else {
                                this.$interact.toast(res.msg)
                            }
                        },
                        fail(err) {
                            this.$interact.toast('未知错误')
                        },
                        complete() {
                            uni.hideLoading()
                        }
                    });
                }
            });
        },
        confirmVideo() {
            this.$emit('active', {
                type: 'video',
                content: this.videoUrl
            })
            this.closeVideoPopup()  
        },
        closeVideoPopup() {
            this.$refs.copyVideo.close()
            this.videoUrl = ''
        }
    }

}
</script>

<style lang="less" scoped>
.add_content{
    position: relative;
    width: 70rpx;
    height: 70rpx;
    .add{
        width: 70rpx;
        height: 70rpx;
        border-radius: 50%;
        background: @mainColor;
        box-shadow: 0 0 20rpx #d4b5b6;
        position: relative;
        z-index: 11;
        .align(center);
        i{
            font-size: 28rpx;
            color: #fff;
            font-weight: bold;
        }
    }
    .add_type{
        position: absolute;
        width: 500rpx;
        height: 150rpx;
        box-shadow: 0 0 10rpx #eee;
        background: #fff;
        top: 80rpx;
        left: 50%;
        transform: translateX(-50%);
        display: flex;
        border-radius: 10rpx;
        z-index: 100;
        .item{
            flex: 1;
            width: 100rpx;
            height: 100%;
            .align(center, column);
            i{
                width: 70rpx;
                height: 70rpx;
                background: #ecd4d5;
                border-radius: 50%;
                font-size: 40rpx;
                font-weight: bold;
            }
            p{
                font-size: 28rpx;
                font-weight: bold;
                margin-top: 10rpx;
            }

        }
    }
}
.enter_word{
    width: 100%;
    height: 100%;
    background: #fff;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 110;
    display: flex;
    flex-direction: column;
    .text{
        position: relative;
        textarea {
            width: 710rpx;
            height: 400rpx;
            border: 1px solid #eee;
            margin: 20rpx auto;
            padding: 20rpx;
        }
        .txt_limit{
            position: absolute;
            right: 30rpx;
            bottom: 30rpx;
            line-height: 30rpx;
            font-size: 28rpx;
            color: #999;
            span{
                color: #000;
            }
        }
    }
    .save_often{
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding: 0 30rpx;
        height: 70rpx;
    }
    .btns{
        padding: 0 30rpx 40rpx;
        justify-content: space-between;
        border-bottom: 20rpx solid #f2f2f2;
        .btn{
            width: 330rpx;
            height: 100rpx;
            background: @mainColor;
            .align(center);
            color: #fff;
            border-radius: 10rpx;
            font-size: 32rpx;
            &:first-child{
                background: #f4f5f7;
                color: #000;
            }
        }
    }
    .often_use{
        width: 100%;
        flex: 1;
        padding: 30rpx;
        overflow: auto;
        .title{
            font-size: 34rpx;
            font-weight: bold;
            margin-bottom: 30rpx;
            justify-content: space-between;
            i{
                font-weight: normal;
                font-size: 28rpx;
                color: @mainColor;
            }
        }
        .content{
            .item{
                width: 100%;
                display: flex;
                align-items: center;
                min-height: 60rpx;
                margin-bottom: 10rpx;
                p{
                    flex: 1;
                    width: 1px;
                    line-height: 40rpx;
                    word-wrap: break-word;
                }

                i{
                    width: 60rpx;
                    height: 60rpx;
                    font-size: 24rpx;
                    color: @mainColor;
                    .align(center);
                }

            }
        }
    }
}

.video_box{
    width: 100%;
    border-radius: 20rpx 20rpx 0 0;
    background: #fff;
    padding: 25rpx;
    position: relative;
    .icon-close{
        position: absolute;
        width: 80rpx;
        height: 80rpx;
        color: #ccc;
        right: 0;
        top: 0;
        .align(center);
    }
    .title{
        text-align: center;
        font-size: 34rpx;
        font-weight: bold;
    }
    textarea{
        width: 100%;
        height: 300rpx;
        margin-top: 30rpx;
        background: #f4f5f7;
        padding: 20rpx;
    }

    .btn{
        width: 100%;
        height: 90rpx;
        background: @mainColor;
        color: #fff;
        border-radius: 10rpx;
        font-size: 32rpx;
        margin-top: 20rpx;
        .align(center);
    }
    .tip{
        margin-top: 50rpx;
        color: #aaa;
    }
    .how_to_use{
        margin-top: 50rpx;
        p{
            margin-top: 30rpx;
            color: #000;
        }

    }
}

.mask{
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 10;
    background: rgba(0, 0, 0, 0.2);
}
</style>